package activities;

import java.util.ArrayList;

import com.iculture.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class MatchActivity extends GlobalActivity {
	private Spinner spinnerGroup;
	private ImageButton imageButtonPlusGroup;
	private TextView textViewFirstOption;
	private TextView textViewSecondOption;
	private TextView textViewThirdOption;
	private LinearLayout linearLayoutFirstOption;
	private LinearLayout linearLayoutSecondOption;
	private LinearLayout linearLayoutThirdOption;
	private TableRow tableRowOptions;
	private TableRow tableRowOptionBar;
	private ViewPager matchPager;
	private ArrayList<View> matchViewList = new ArrayList<View>(3);
	private int matchViewSize;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_match);
		findView();
		setListenerOrAdapter();
		buttonMatch.setClickable(false);
		setAllOptions();
		setIgroupOptions();
		yieldIgroupView();
	}

	@Override
	protected void findView() {
		super.findView();
		spinnerGroup = (Spinner)findViewById(R.id.spinnerGroup);
		imageButtonPlusGroup = (ImageButton)findViewById(R.id.imageButtonPlusGroup);
		tableRowOptions = (TableRow)findViewById(R.id.tableRowOptions);
		tableRowOptionBar = (TableRow)findViewById(R.id.tableRowOptionBar);
		textViewFirstOption = new TextView(MatchActivity.this);
		textViewSecondOption = new TextView(MatchActivity.this);
		textViewThirdOption = new TextView(MatchActivity.this);
		linearLayoutFirstOption = new LinearLayout(MatchActivity.this);
		linearLayoutSecondOption = new LinearLayout(MatchActivity.this);
		linearLayoutThirdOption = new LinearLayout(MatchActivity.this);
		matchPager = (ViewPager)findViewById(R.id.matchPager);
	}
	
	@Override
	protected void setListenerOrAdapter() {
		String[] spinnerGroupItems = {"我揪的團", "別人揪的團"};
		
		super.setListenerOrAdapter();
		spinnerGroup.setAdapter(new ArrayAdapter<String>(MatchActivity.this, android.R.layout.simple_spinner_dropdown_item, spinnerGroupItems));
		spinnerGroup.setOnItemSelectedListener(new SpinnerGroupOnItemSelectedListener());
		imageButtonPlusGroup.setOnClickListener(new ImageButtonPlusGroupOnClickListener());
		textViewFirstOption.setOnClickListener(new TextViewFirstOptionOnClickListener());
		textViewSecondOption.setOnClickListener(new TextViewSecondOptionOnClickListener());
		textViewThirdOption.setOnClickListener(new TextViewThirdOptionOnClickListener());
		matchPager.setAdapter(new MatchPagerAdapter());
		matchPager.setOnPageChangeListener(new MatchPagerOnPageChangeListener());
	}
	
	private class SpinnerGroupOnItemSelectedListener implements Spinner.OnItemSelectedListener {
		private int preIndex = 0;

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int itemIndex, long arg3) {
			if (preIndex != 0 && itemIndex == 0) {
				imageButtonPlusGroup.setVisibility(ImageButton.VISIBLE);
				tableRowOptions.removeAllViews();
				tableRowOptionBar.removeAllViews();
				setIgroupOptions();
				matchViewList.clear();
				yieldIgroupView();
				preIndex = 0;
			}
			else if (preIndex != 1 && itemIndex == 1) {
				imageButtonPlusGroup.setVisibility(ImageButton.INVISIBLE);
				tableRowOptions.removeAllViews();
				tableRowOptionBar.removeAllViews();
				setOthergroupOptions();
				matchViewList.clear();
				yieldOtherGroupView();
				preIndex = 1;
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
	
	private class ImageButtonPlusGroupOnClickListener implements ImageButton.OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			
			intent.setClass(MatchActivity.this, AddMatchGroupActivity.class);
			startActivity(intent);
		}
	}
	
	private class TextViewFirstOptionOnClickListener implements TextView.OnClickListener {

		@Override
		public void onClick(View v) {
			chooseOption(1);
		}
	}
	
	private class TextViewSecondOptionOnClickListener implements TextView.OnClickListener {

		@Override
		public void onClick(View v) {
			chooseOption(2);
		}
	}
	
	private class TextViewThirdOptionOnClickListener implements TextView.OnClickListener {

		@Override
		public void onClick(View v) {
			chooseOption(3);
		}
	}
	
	private class MatchPagerAdapter extends PagerAdapter {
		
		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(matchViewList.get(arg1));
		}

		@Override
		public int getCount() {
			return matchViewSize;
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(matchViewList.get(arg1), 0);
			return matchViewList.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
    }
	
	private class MatchPagerOnPageChangeListener implements ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int pageIndex) {
			chooseOption(pageIndex + 1);
		}
	}
	
	private void chooseOption(int which) {
		linearLayoutFirstOption.setBackgroundColor(getResources().getColor(R.color.black));
		linearLayoutSecondOption.setBackgroundColor(getResources().getColor(R.color.black));
		linearLayoutThirdOption.setBackgroundColor(getResources().getColor(R.color.black));
		switch (which) {
			case 1:
				linearLayoutFirstOption.setBackgroundColor(getResources().getColor(R.color.gold));
				break;
			case 2:
				linearLayoutSecondOption.setBackgroundColor(getResources().getColor(R.color.gold));
				break;
			case 3:
				linearLayoutThirdOption.setBackgroundColor(getResources().getColor(R.color.gold));
		}
		matchPager.setCurrentItem(which - 1);
	}
	
	private void setAllOptions() {
		textViewFirstOption.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
		textViewSecondOption.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
		textViewThirdOption.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
		textViewFirstOption.setGravity(Gravity.CENTER);
		textViewSecondOption.setGravity(Gravity.CENTER);
		textViewThirdOption.setGravity(Gravity.CENTER);
		textViewFirstOption.setTextSize(25);
		textViewSecondOption.setTextSize(25);
		textViewThirdOption.setTextSize(25);
		linearLayoutFirstOption.setLayoutParams(new TableRow.LayoutParams(0, 5, 1));
		linearLayoutSecondOption.setLayoutParams(new TableRow.LayoutParams(0, 5, 1));
		linearLayoutThirdOption.setLayoutParams(new TableRow.LayoutParams(0, 5, 1));
	}
	
	private void setIgroupOptions() {
		textViewFirstOption.setText("進行中");
		textViewSecondOption.setText("已結束");
		tableRowOptions.addView(textViewFirstOption);
		tableRowOptions.addView(textViewSecondOption);
		linearLayoutFirstOption.setBackgroundColor(getResources().getColor(R.color.gold));
		linearLayoutSecondOption.setBackgroundColor(getResources().getColor(R.color.black));
		tableRowOptionBar.addView(linearLayoutFirstOption);
		tableRowOptionBar.addView(linearLayoutSecondOption);
	}
	
	private void setOthergroupOptions() {
		textViewFirstOption.setText("參加");
		textViewSecondOption.setText("未答覆");
		textViewThirdOption.setText("未參加");
		tableRowOptions.addView(textViewFirstOption);
		tableRowOptions.addView(textViewSecondOption);
		tableRowOptions.addView(textViewThirdOption);
		linearLayoutFirstOption.setBackgroundColor(getResources().getColor(R.color.gold));
		linearLayoutSecondOption.setBackgroundColor(getResources().getColor(R.color.black));
		linearLayoutThirdOption.setBackgroundColor(getResources().getColor(R.color.black));
		tableRowOptionBar.addView(linearLayoutFirstOption);
		tableRowOptionBar.addView(linearLayoutSecondOption);
		tableRowOptionBar.addView(linearLayoutThirdOption);
	}
	
	private void yieldIgroupView() {
		for (int i=0; i<2; i++)
			matchViewList.add(getLayoutInflater().inflate(R.layout.match_table, null));
		matchViewSize = 2;
		matchPager.setCurrentItem(0);
	}
	
	private void yieldOtherGroupView() {
		for (int i=0; i<3; i++)
			matchViewList.add(getLayoutInflater().inflate(R.layout.match_table, null));
		matchViewSize = 3;
		matchPager.setCurrentItem(0);
	}
}